package com.linxiao.hrcrm.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.linxiao.hrcrm.domain.CrmCustomer;
import com.linxiao.hrcrm.service.ICrmCustomerService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import com.linxiao.hrcrm.mapper.CrmCustomerRelationshipMapper;
import com.linxiao.hrcrm.domain.CrmCustomerRelationship;
import com.linxiao.hrcrm.service.ICrmCustomerRelationshipService;

import javax.annotation.Resource;

/**
 * 客户关系Service业务层处理
 *
 * @author zhoudong
 * @date 2024-01-03
 */
@Service
public class CrmCustomerRelationshipServiceImpl implements ICrmCustomerRelationshipService {
    @Resource
    private CrmCustomerRelationshipMapper crmCustomerRelationshipMapper;

    @Resource
    private ICrmCustomerService crmCustomerService;

    @Resource
    private ISysUserService sysUserService;

    /**
     * 查询客户关系
     *
     * @param id 客户关系主键
     * @return 客户关系
     */
    @Override
    public CrmCustomerRelationship selectCrmCustomerRelationshipById(Long id) {
        return crmCustomerRelationshipMapper.selectCrmCustomerRelationshipById(id);
    }

    /**
     * 查询客户关系列表
     *
     * @param crmCustomerRelationship 客户关系
     * @param isSelectCustomer
     * @return 客户关系
     */
    @Override
    public List<CrmCustomerRelationship> selectCrmCustomerRelationshipList(CrmCustomerRelationship crmCustomerRelationship, Boolean isSelectCustomer) {
        List<CrmCustomerRelationship> crmCustomerRelationships = crmCustomerRelationshipMapper.selectCcrListJoinCustomer(crmCustomerRelationship);
        if (CollUtil.isNotEmpty(crmCustomerRelationships)) {
            crmCustomerRelationships.parallelStream().forEach(ccr -> {
                if (StrUtil.isNotBlank(ccr.getTags())) {
                    String[] split = ccr.getTags().split(",", -1);
                    List<String> tagList = Arrays.asList(split);
                    ccr.setTagList(tagList);
                }
                if (isSelectCustomer) {
                    CrmCustomer crmCustomer = crmCustomerService.selectCrmCustomerById(ccr.getCustomerId());
                    ccr.setCustomer(crmCustomer);
                }
                // 查询员工名称
                SysUser user = sysUserService.selectUserById(ccr.getEmployeeId());
                ccr.setEmployeeName(user.getNickName());
            });
        }
        return crmCustomerRelationships;
    }

    /**
     * 新增客户关系
     *
     * @param crmCustomerRelationship 客户关系
     * @return 结果
     */
    @Override
    public int insertCrmCustomerRelationship(CrmCustomerRelationship crmCustomerRelationship) {
        if(StrUtil.isNotBlank(crmCustomerRelationship.getTags()) && crmCustomerRelationship.getTags().contains("，")) {
            crmCustomerRelationship.setTags(crmCustomerRelationship.getTags().replace("，", ","));
        }
        crmCustomerRelationship.setCreateTime(DateUtils.getNowDate());
        return crmCustomerRelationshipMapper.insertCrmCustomerRelationship(crmCustomerRelationship);
    }

    /**
     * 修改客户关系
     *
     * @param crmCustomerRelationship 客户关系
     * @return 结果
     */
    @Override
    public int updateCrmCustomerRelationship(CrmCustomerRelationship crmCustomerRelationship) {
        if(StrUtil.isNotBlank(crmCustomerRelationship.getTags()) && crmCustomerRelationship.getTags().contains("，")) {
            crmCustomerRelationship.setTags(crmCustomerRelationship.getTags().replace("，", ","));
        }
        crmCustomerRelationship.setUpdateTime(DateUtils.getNowDate());
        return crmCustomerRelationshipMapper.updateCrmCustomerRelationship(crmCustomerRelationship);
    }

    /**
     * 批量删除客户关系
     *
     * @param ids 需要删除的客户关系主键
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerRelationshipByIds(Long[] ids) {
        return crmCustomerRelationshipMapper.deleteCrmCustomerRelationshipByIds(ids);
    }

    /**
     * 删除客户关系信息
     *
     * @param id 客户关系主键
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerRelationshipById(Long id) {
        return crmCustomerRelationshipMapper.deleteCrmCustomerRelationshipById(id);
    }

    /**
     * 分配
     * @param mchId
     * @param employeeId
     * @param ids
     * @return
     */
    @Override
    public int allocation(Long mchId, Long employeeId, List<Long> ids) {
        AtomicInteger result = new AtomicInteger();
        ids.parallelStream().forEach(id -> {
            CrmCustomerRelationship relationship = new CrmCustomerRelationship();
            relationship.setCustomerId(id);
            relationship.setMchId(mchId);
            relationship.setEmployeeId(employeeId);
            relationship.setSource("管理员分配");
            relationship.setRelationshipStatus(CrmCustomerRelationship.RELATIONSHIP_STATUS_ACTIVE);
            relationship.setCustomerState(CrmCustomerRelationship.CUSTOMER_STATE_WEILIANXI);
            insertCrmCustomerRelationship(relationship);
            result.getAndIncrement();
        });
        return result.get();
    }
}
